﻿using AJWPFAdmin.Core.Enums;
using AJWPFAdmin.Core.Logger;
using AJWPFAdmin.Core.Models.EF.Tables;
using AJWPFAdmin.Core.Utils;
using AJWPFAdmin.Services.AppointAPI.ChengDu;
using AJWPFAdmin.Services.EF;
using AJWPFAdmin.Services.Migrations;
using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;
using Masuit.Tools;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Prism.Ioc;
using Quartz.Impl.AdoJobStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using WebApiClientCore.Serialization.JsonConverters;

namespace AJWPFAdmin.Services.AppointAPI
{
    public class AppointAPISvc
    {
        private AppointAPIBase _api;
        public AppointAPIBase API
        {
            get => _api;
            private set => _api = value;
        }

        public AppointAPISvc(ApplicationType appType, IContainerProvider svcProvider)
        {
            var key = SystemConfigKey.APIDefault;

            SystemConfigKey configKey = key ;

            using var db = DbService.GetDbContext(svcProvider.Resolve<AJConfigService>());

            var cfg = db.SystemConfigDictionaries.AsNoTracking()
                .FirstOrDefault(p => p.Key >= SystemConfigKey.APIDefault && p.BooleanValue);

            if (cfg != null)
            {
                configKey = cfg.Key;
                key = cfg.Key;
            }

            var _closeClientAPI = false;

            if (appType == ApplicationType.岗亭端 && key != SystemConfigKey.APIDefault)
            {
                var defaultCfg = db.SystemConfigDictionaries.AsNoTracking().FirstOrDefault(p => p.Key == SystemConfigKey.APIDefault);

                var defaultAPICfg = CommonUtil.TryGetJSONObject<DefaultAppointAPI.APIConfig>(defaultCfg?.StringValue);
                _closeClientAPI = (defaultAPICfg?.CloseClientAPI).GetValueOrDefault();
                if (defaultAPICfg == null || _closeClientAPI)
                {
                    key = SystemConfigKey.APIDefault;
                }
            }

            switch (key)
            {
                case SystemConfigKey.APITangShan:
                    API = new TangShanAppointAPI(appType, cfg, svcProvider);
                    break;
                case SystemConfigKey.APILangFang:
                    API = new LangfangAppointAPI(appType, cfg, svcProvider);
                    break;
                case SystemConfigKey.APIGanZhou:
                    API = new GanzhouAppointAPI(appType, cfg, svcProvider);
                    break;
                case SystemConfigKey.APIQingDao:
                    API = new QingdaoAppointAPI(appType, cfg, svcProvider);
                    break;
                case SystemConfigKey.APILinYi:
                    API = new LinyiAppointAPI(appType, cfg, svcProvider);
                    break;
                case SystemConfigKey.APIBinZhou:
                    API = new BinZhouAppointAPI(appType, cfg, svcProvider);
                    break;
                case SystemConfigKey.APIChengDu:
                    API = new ChengDuAppointAPI(appType, cfg, svcProvider);
                    break;
                case SystemConfigKey.APIBeiJing:
                    API = new BeiJingAppointAPI(appType, cfg, svcProvider);
                    break;
                case SystemConfigKey.APIQinYou:
                    API = new QinYouAppointAPI(appType, cfg, svcProvider);
                    break;
                case SystemConfigKey.APIDeYang:
                    API = new DeyangAppointAPI(appType, cfg, svcProvider);
                    break;
                default:
                    API = new DefaultAppointAPI(appType, cfg, svcProvider);
                    break;
            }
            API.ConvertToCustomConfig(true);
            if (_closeClientAPI)
            {
                (API as DefaultAppointAPI).SetFakeConfig(configKey);
            }
        }

        public static void Init(IServiceCollection services)
        {
            try
            {
                var key = SystemConfigKey.APIDefault;
                var svcProvider = services.BuildServiceProvider();
                using var db = DbService.GetDbContext(svcProvider.GetService<AJConfigService>());
                var cfg = db.SystemConfigDictionaries.AsNoTracking()
                    .FirstOrDefault(p => p.Key >= SystemConfigKey.APIDefault && p.BooleanValue);

                if (cfg != null)
                {
                    key = cfg.Key;
                }
                switch (key)
                {
                    case SystemConfigKey.APIGanZhou:
                        services.AddHttpApi<IGanzhouApiImplement>();
                        services.AddSingleton<GetGanzhouToken>();
                        break;
                    case SystemConfigKey.APIQingDao:
                        services.AddHttpApi<IQingdaoApiImplement>();
                        services.AddSingleton<GetQingdaoToken>();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {


            }
        }
    }
}
